var thesum=0;    //跟进内容返回数量
var resdata=[];  //跟进内容返回数据
var theadd = 0;
var parentJson = {};
parentJson.limit=1000;
parentJson.headField="pno,content,meetingtheme,replydate,enddate,plantype,mno,ino,completedate,finishdate,flag,jindu,replycontent,img,checkuser,decisionuser,emergency,level,expecttime,plantime,description,conclusion";
parentJson.page=1;
var username = sessionStorage.getItem("name"); 
var userid = sessionStorage.getItem("userId"); 
var now = getNowFormatDate();
$('#name').text(sessionStorage.getItem("name"));
var _config ;
var addtof =true;
var photoUrl = [];//拍照列表
var sendData = {};  //待办事项
sendData.planuserid = sessionStorage.getItem("userId");
sendData.flag=10;
var url  = location.search;
var theRequest = new Object();  
if ( url.indexOf( "?" ) != -1 ){  
var str = url.substr( 1 ); //substr()方法返回从参数值开始到结束的字符串；  
var strs = str.split( "&" ); 
var plantype=1; 
var pno,mno,ino;
for ( var i = 0; i < strs.length; i++ ) {  
theRequest[ strs[ i ].split( "=" )[ 0 ] ] = ( strs[ i ].split( "=" )[ 1 ] );  
}  
} 

pno=theRequest.pno;
sendData.pno =theRequest.pno; 
var aa={};
aa.conditionField = "pno";
aa.conditionValue = theRequest.pno;
aa.conditionOption = "=";
aa.logicalOperator = "and";
parentJson.search=[aa];
$.ajax({
	url: "../../work_plan_info/getSelectBody"
	,type:'post'
	,async: false
	,contentType: "application/json; charset=utf-8"
	,data:JSON.stringify(parentJson) 
	,dataType:'json'
	,success: function(res){
		plantype=res.data[0].plantype;
		mno=res.data[0].mno;
		ino=res.data[0].ino;
		if(res.data[0].content==undefined){
			wcontent.value="";
		}else{wcontent.value=res.data[0].content;}
		if(res.data[0].enddate==undefined){
			enddate.value="";
		}else{
			enddate.value=res.data[0].enddate;
		}
		if(res.data[0].level==undefined){
			level.value="";
		}else{
			level.value=res.data[0].level;
		}
		if(res.data[0].expecttime==undefined){
			expecttime.value="";
		}else{
			expecttime.value=res.data[0].expecttime;
		}
		if(res.data[0].plantime==undefined){
			plantime.value="";
		}else{
			plantime.value=res.data[0].plantime;
		}
		if(res.data[0].checkuser==undefined){
			checkuser.value="";
		}else{
			checkuser.value=res.data[0].checkuser;
		}
		if(res.data[0].description==undefined){
			description.value="";
		}else{
			description.value=res.data[0].description;
		}
	}
});
$.ajax({              //待办事项
	url: "../../meeting_info_detail/getdetailBody"
	,type:'post'
	,async: false
	,contentType: "application/json; charset=utf-8"
	,data:JSON.stringify(sendData) 
	,dataType:'json'
	,success: function(res){
		resdata=res.data;	
		if(res.data2 != undefined){mno =res.data2}else{
			if(res.data==''){mno=0;}
			else{mno = res.data[0].mno}
		}
		if(res.data3 != undefined){ino =res.data3}else{
			if(res.data==''){ino=0;}
			else{ino = res.data[0].ino}
		}
        if(res.data.length!=0){
		if(res.data[resdata.length-1].lrsj.substring(0,10)==getNowFormatDate()){addtof=false;};
		}
		thesum=resdata.length;
       if(res.data.length==0){ addform();}  //如果没有数据 则默认添加一条
	},error:function(XMLHttpRequest, textStatus, errorThrown) {
		 alert(JSON.stringify(XMLHttpRequest));
		 alert(JSON.stringify(errorThrown));
		 alert(textStatus);
	}
});
function ICalendarClick(){
	
}
$(document).ready(function(){
	daiban();
	$('#addButton').on('click',addform);
	$('#searchButton').on('click',saveInfo);
	$('#searchButton1').on('click',finishSVInfo);
});	
//获取当前日期
function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
}
function daiban(){  //待办事项
			for(var i=0;i<thesum;i++){	
				if(resdata[i].jindu==undefined){
				resdata[i].jindu="";
			}	
				
	$('#weiDiv').append('<div class="widget-header widget-header-flat widget-header-small">'   //标题
					+'<h5 style="font-weight:bold;color:#eb6100;">跟进内容'+(i+1)+'</h5>'
					+'</div>');
	$('#weiDiv').append('<div class="col_box">'                                               //内容
						+'<div>'		
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'				
						+'<h5><i class="iconfont">&#xe609;</i>&nbsp;跟进时间<span class="red"></span></h5>'					
						+'<input type="text"  id="enddate'+i+'"  autocomplete="off"   class="pull-right  layui-input  add_bor_none" style="width: 70%;margin-right: 2%;border:none" readonly="readonly" value="'+resdata[i].lrsj+'"/>'					    
						+'</div>'				
						+'</div>'			
						+'</div>'								
						+'<div>'							
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5 id="wentishuomingTitle"><i class="iconfont">&#xe600;</i>&nbsp;跟进内容</h5><span class="red"></span>'
						+'</div>'
						+'<div class="widget-body zhediebox1">'
						+'<div class="widget-main pd_div" style="padding:0;">'
						+'<div class="clearfix jy_div" style="padding:0;">'
						+'<textarea id="content'+i+'"  rows="" cols="" class="pull-left  add_pad_tb10p add_bor_none" '+setreadonly(resdata[i])+' >'+resdata[i].replycontent+'</textarea>'
						+'</div>'
						+'</div></div></div></div>'			
						+'<div>'		
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'       			
						+'<h5><i class="iconfont">&#xe600;</i>&nbsp;进度<span class="red"></span></h5>'	
						+'<input type="text" id="jindu'+i+'" class="pull-right add_bor_none" autocomplete="off" style="width: 78%;margin-right: 2%;border:none" '+setreadonly(resdata[i])+' value="'+resdata[i].jindu+'" />'
						+'</div>'	
						+'</div>'
						+'</div>'
						+'<div>'							
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5 id="wentishuomingTitle"><i class="iconfont">&#xe600;</i>&nbsp;心得体会</h5><span class="red"></span>'
						+'</div>'
						+'<div class="widget-body zhediebox1">'
						+'<div class="widget-main pd_div" style="padding:0;">'
						+'<div class="clearfix jy_div" style="padding:0;">'
						+'<textarea id="conclusion'+i+'"  rows="" cols="" class="pull-left add_pad_tb10p add_bor_none" '+setreadonly(resdata[i])+' >'+resdata[i].conclusion+'</textarea>'
						+'</div>'
						+'</div></div></div></div>'	
							
						+'</div>'
	);
}
}
function addform(){  
   if(addtof){
    addtof=false;
	var i=thesum;			
	$('#weiDiv').append('<div class="widget-header widget-header-flat widget-header-small">'  
					+'<h5 style="font-weight:bold;color:#eb6100;">跟进内容'+(i+1)+'</h5>'
					+'</div>');
	$('#weiDiv').append('<div class="col_box">'                                           
						+'<div>'		
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'				
						+'<h5><i class="iconfont">&#xe609;</i>&nbsp;跟进时间<span class="red"></span></h5>'					
						+'<input type="text"  id="enddate"  autocomplete="off"   class="pull-right layui-input add_bor_none" style="width: 70%;margin-right: 2%;border:none" readonly="readonly" value="'+getNowFormatDatetime()+'"/>'					    
						+'</div>'				
						+'</div>'			
						+'</div>'								
						+'<div>'							
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5 id="wentishuomingTitle"><i class="iconfont">&#xe600;</i>&nbsp;跟进内容</h5><span class="red"></span>'
						+'</div>'
						+'<div class="widget-body zhediebox1">'
						+'<div class="widget-main pd_div" style="padding:0;">'
						+'<div class="clearfix jy_div" style="padding:0;">'
						+'<textarea id="content"  rows="" cols="" class="pull-left  add_pad_tb10p add_bor_none" ></textarea>'
						+'</div>'
						+'</div></div></div></div>'			
						+'<div>'		
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'       			
						+'<h5><i class="iconfont">&#xe600;</i>&nbsp;进度<span class="red"></span></h5>'	
						+'<input type="text" id="jindu" class="pull-right" autocomplete="off" style="width: 78%;margin-right: 2%;border:none"   />'
						+'</div>'	
						+'</div>'
						+'</div>'
						+'<div>'							
						+'<div class="widget-box add_mar_tb7p">'			
						+'<div class="widget-header widget-header-flat widget-header-small add_pad_tb10p add_bor_tbgrey">'
						+'<h5 id="wentishuomingTitle"><i class="iconfont">&#xe609;</i>&nbsp;心得体会</h5><span class="red"></span>'
						+'</div>'
						+'<div class="widget-body zhediebox1">'
						+'<div class="widget-main pd_div" style="padding:0;">'
						+'<div class="clearfix jy_div" style="padding:0;">'
						+'<textarea id="conclusion"  rows="" cols="" class="pull-left add_pad_tb10p add_bor_none" ></textarea>'
						+'</div>'
						+'</div></div></div></div>'	
							
						+'</div>'
	);
	theadd=thesum+1;
  }else{alert("每日只能新增一条跟进内容");}
}
function setreadonly(resdata){
	if(resdata.lrsj.substring(0,10)==getNowFormatDate()){return ''; }else{return 'readonly="readonly"';}
}
function saveInfo(){
	showloading();
	try {
		var imgStr = "";
		var sendData = {};
		var insertArray = [];
		var updateArray = [];
		var updateArrayDetail = [];
		var updateJson = {};
		var insertJson = {};
		var detail = {};
        for(var i=0;i<thesum;i++){
	        var replycontent =  document.getElementById('content'+i).value;
			var lrsj =  document.getElementById('enddate'+i).value;
			var jindu =  document.getElementById('jindu'+i).value;
			var conclusion =  document.getElementById('conclusion'+i).value;
			updateJson.replycontent=replycontent;
			updateJson.lrsj=lrsj;
			updateJson.jindu=jindu;
			updateJson.conclusion=conclusion;
			updateJson.pno=pno;
			updateJson.mno=mno;
			updateJson.ino=ino;
			updateArray.push(updateJson);
			detail.mno=mno;
			detail.ino=ino;
		if(detail.mno==0&&detail.ino==0){detail.pno=pno;}
			detail.jindu=jindu;
			detail.flag=10;
			detail.finishdate="";
		}
		if(theadd!=0){
		    replycontent =document.getElementById('content').value;
			lrsj =document.getElementById('enddate').value;
			jindu =document.getElementById('jindu').value;
			conclusion =document.getElementById('conclusion').value;
			insertJson.replycontent=replycontent;
			insertJson.lrsj=lrsj;
			insertJson.lrr=userid;
			insertJson.xgr=userid;
			insertJson.jindu = jindu;
			insertJson.conclusion=conclusion;
			insertJson.pno=pno;
			insertJson.mno=mno;
			insertJson.ino=ino;
			insertArray.push(insertJson);
			detail.mno=mno;
			detail.ino=ino;
		if(detail.mno==0&&detail.ino==0){detail.pno=pno;}
			detail.jindu=jindu;
			detail.flag=10;
			detail.finishdate="";
		}
	   updateArrayDetail.push(detail);
       sendData.insertArray=insertArray;
   	   sendData.updateArray=updateArray;
       sendData.updateArrayDetail=updateArrayDetail;
      console.log(sendData);
	  $.ajax({
				url: "../../meeting_info_detail/replyedit"
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(sendData) 
				,success: function(res){
					console.log(res)
					if(res.status==1000){
						layer.msg(res.msg);
					}else{
						layer.msg('保存成功',{time:1000},function(){
							dd.device.base.getPhoneInfo({
							    onSuccess : function(data) {//iPhone
			                    if(data.brand == iPhone){
									sessionStorage.setItem("Creload", true);
								}
							        /*
							        {
							            screenWidth: 1080, // 手机屏幕宽度
							            screenHeight: 1920, // 手机屏幕高度
							            brand:'Mi', // 手机品牌
							            model:'Note4', // 手机型号
							            version:'7.0', // 版本
							            netInfo:'wifi', // 网络类型 wifi／4g／3g 
							            operatorType:'xx' // 运营商信息
							        }
							        */
							    },
							    onFail : function(err) {}
							});
							history.go(-1);
							/*url = "../../mobile_page/work_plan_info/work_content_search";//此处拼接内容
	                        window.location.href = url;*/
						});
					}
				}
			    });
	} catch (e) {
		console.log(e)
	} finally{
		hideloading();
	}	
}
function finishSVInfo(){
	sessionStorage.setItem("Creload", true);
	showloading();
	try {
		var imgStr = "";
		var sendData = {};
		var insertArray = [];
		var updateArray = [];
		var updateArrayDetail = [];
		var updateJson = {};
		var insertJson = {};
		var detail = {};
        for(var i=0;i<thesum;i++){
	        var replycontent =  document.getElementById('content'+i).value;
			var lrsj =  document.getElementById('enddate'+i).value;
			var jindu =  document.getElementById('jindu'+i).value;
			updateJson.replycontent=replycontent;
			updateJson.lrsj=lrsj;
			updateJson.jindu=jindu;
			updateJson.pno=pno;
			updateJson.mno=mno;
			updateJson.ino=ino;
			updateArray.push(updateJson);
			detail.mno=mno;
			detail.ino=ino;
			if(detail.mno==0&&detail.ino==0){detail.pno=pno;}
			detail.jindu=jindu;
			detail.flag=9;
			detail.finishdate=getNowFormatDatetime();
		}
		if(theadd!=0){
		    replycontent =document.getElementById('content').value;
			lrsj =document.getElementById('enddate').value;
			jindu =document.getElementById('jindu').value;
			insertJson.replycontent=replycontent;
			insertJson.lrsj=lrsj;
			insertJson.jindu = jindu;
			insertJson.pno=pno;
			insertJson.mno=mno;
			insertJson.ino=ino;
			insertArray.push(insertJson);
			detail.mno=mno;
			detail.ino=ino;
			if(detail.mno==0&&detail.ino==0){detail.pno=pno;}
			detail.jindu=jindu;
			detail.flag=9;
			detail.finishdate=getNowFormatDatetime();
		}
	   updateArrayDetail.push(detail);
       sendData.insertArray=insertArray;
   	   sendData.updateArray=updateArray;
       sendData.updateArrayDetail=updateArrayDetail;
       console.log(sendData);
	 $.ajax({
				url: "../../meeting_info_detail/replyedit"
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(sendData) 
				,success: function(res){
					console.log(res)
					if(res.status==1000){
						layer.msg(res.msg);
					}else{
						layer.msg('该计划已完成',{time:1000},function(){
						dd.device.base.getPhoneInfo({
							    onSuccess : function(data) {//iPhone
			                    if(data.brand == iPhone){
									sessionStorage.setItem("Creload", true);
								}
							        /*
							        {
							            screenWidth: 1080, // 手机屏幕宽度
							            screenHeight: 1920, // 手机屏幕高度
							            brand:'Mi', // 手机品牌
							            model:'Note4', // 手机型号
							            version:'7.0', // 版本
							            netInfo:'wifi', // 网络类型 wifi／4g／3g 
							            operatorType:'xx' // 运营商信息
							        }
							        */
							    },
							    onFail : function(err) {}
							});
							history.go(-1);
							/*url = "../../mobile_page/work_plan_info/work_content_search";//此处拼接内容
	                        window.location.href = url;*/
						});
					}
				}
			    });
	} catch (e) {
		console.log(e)
	} finally{
		hideloading();
	}	
}
//获取当前日期
function getNowFormatDatetime() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
    	var strDate = date.getDate();
		var hour = date.getHours();
		var minutes = date.getMinutes();
		var seconds = date.getSeconds();
    
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }	
		if (hour >= 0 && hour <= 9) {
            hour = "0" + hour;
        }
  		if (minutes >= 0 && minutes <= 9) {
            minutes = "0" + minutes;
        }
  		if (seconds >= 0 && seconds <= 9) {
            seconds = "0" + seconds;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate + ' '+hour+':'+minutes+':'+seconds;
        return currentdate;
    }